package network.step2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SimpleServer2 {
	public static void main(String[] args) throws IOException{
		ServerSocket serverSocket = new ServerSocket(5000);
		
		Socket socket = null;
		BufferedReader br = null;
		PrintWriter out = null;
		
		while(true){
			try{
				socket = serverSocket.accept();
				
				br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				out = new PrintWriter(socket.getOutputStream(), true);
				
				String str = br.readLine();
				while(str != null){
					System.out.println(str);
					out.println(str);//한번 읽은걸 두번 출력하는거지!!!
					//out.flush();
					//하나는 콘솔에다가 출력하고, 다른 하나는 println으로 출력하는거고
					str = br.readLine();
				}

				
			}catch(IOException e){
				e.printStackTrace();
			}finally{
				if(br != null) br.close();
				if(socket != null)socket.close();
				if(out != null)out.close();
			}
		}
		
	}
}
